package edu.gatech.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import edu.gatech.datastorage.TreatmentRecord;
import edu.gatech.presenters.MedicalHistoryPresenter;
import edu.gatech.userclasses.Patient;

public class MedicalHistoryPanel {
	private static JTextArea textArea;
	private static Patient currentPatient;
	private static TreatmentRecord record;
	public MedicalHistoryPanel(Patient curr){
		currentPatient = curr;
		ArrayList<TreatmentRecord> list = (ArrayList<TreatmentRecord>) currentPatient.getInfo().getHistory();
		String[] recordList = new String[list.size()];
		for(int i = 0; i < list.size(); i++){
			recordList[i] = list.get(i).getAppointment().getDate();
		}
		JFrame history = new JFrame("Medical History");
		
		MenuBar menuBar = new MenuBar(history);
		history.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        history.setSize(600,480); 
        history.getContentPane().setLayout(null);
        
        JComboBox comboBox = new JComboBox(recordList);
        comboBox.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		JComboBox cb = (JComboBox)e.getSource();
                String selectedRecord = (String)cb.getSelectedItem();
                MedicalHistoryPresenter presenter = new MedicalHistoryPresenter(record, currentPatient);
                presenter.updateRecordDisplay(selectedRecord, textArea);
        	}
        });
		comboBox.setBounds(63, 217, 221, 21);
		comboBox.setFont(new Font("Arial", Font.PLAIN, 10));
        history.getContentPane().add(comboBox);
        
        record = currentPatient.getInfo().getLastRecord();
        String selected = (String) comboBox.getSelectedItem();
        for(int i = 0; i < list.size(); i++){
        	if(list.get(i).getAppointment().getDate().equals(selected)){
        		record = list.get(i);
        	}
        }
        textArea = new JTextArea();
        textArea.setEditable(false);
        if(record != null){
        	textArea.setText(record.toString());
        }
        else{
        	textArea.setText("No treatment record on file");
        }
        textArea.setFont(new Font("Arial", Font.PLAIN, 10));
        textArea.setBounds(294, 140, 264, 274);
        history.getContentPane().add(textArea);
        textArea.setColumns(10);
        
        JLabel lblCurrentPatient = new JLabel("Current Patient:");
        lblCurrentPatient.setFont(new Font("Arial", Font.PLAIN, 10));
        lblCurrentPatient.setBounds(34, 34, 90, 14);
        history.getContentPane().add(lblCurrentPatient);
        
        JLabel label = new JLabel(currentPatient.getName());
        label.setFont(new Font("Arial", Font.BOLD, 14));
        label.setBounds(34, 59, 160, 14);
        history.getContentPane().add(label);
        
        JLabel lblUpcomingAppointment = new JLabel("Upcoming Appointment:");
        lblUpcomingAppointment.setFont(new Font("Arial", Font.PLAIN, 10));
        lblUpcomingAppointment.setBounds(264, 46, 279, 14);
        history.getContentPane().add(lblUpcomingAppointment);
        
        JLabel lblNewLabel_1 = new JLabel(currentPatient.getInfo().getLastRecord().getAppointment().toString());
        lblNewLabel_1.setFont(new Font("Arial", Font.BOLD, 10));
        lblNewLabel_1.setBounds(264, 72, 279, 14);
        history.getContentPane().add(lblNewLabel_1);
        
        JPanel panel = new JPanel();
        panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.setBounds(252, 34, 306, 63);
        history.getContentPane().add(panel);
        
        JLabel lblRecords = new JLabel("Records:");
        lblRecords.setFont(new Font("Arial", Font.PLAIN, 10));
        lblRecords.setBounds(19, 220, 46, 14);
        history.getContentPane().add(lblRecords);
        
        JButton btnViewHistoryGraph = new JButton("View History Graph");
        btnViewHistoryGraph.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent arg0) {
        		LineGraph graph = new LineGraph("History Graph");
        	}
        });
        btnViewHistoryGraph.setFont(new Font("Arial", Font.PLAIN, 10));
        btnViewHistoryGraph.setBounds(34, 255, 179, 23);
        history.getContentPane().add(btnViewHistoryGraph);
        
        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Treatment Record", TitledBorder.LEADING, TitledBorder.TOP, null, UIManager.getColor("CheckBox.darkShadow")));
        panel_2.setBounds(10, 116, 564, 315);
        history.getContentPane().add(panel_2);
        history.setVisible(true);
	}
}
